package solysombra.api.resources;
import java.net.URI;
import java.util.Calendar;
import java.util.Collection;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;
import org.jboss.resteasy.spi.BadRequestException;
import org.jboss.resteasy.spi.NotFoundException;
import solysombra.domain.Place;
import solysombra.domain.PlaceList;
import solysombra.repository.MapPlaceListRepository;
import solysombra.repository.PlaceListRepository;
@Path("/places")
public class PlaceListResource {
//singleton
private static PlaceListResource _instance=null;
PlaceListRepository repository;
private PlaceListResource() {
repository=new MapPlaceListRepository();
initialize();
}
public static PlaceListResource getInstance()
{
if(_instance==null)
_instance=new PlaceListResource();
return _instance;
}
//a�adimos datos para el test en el repositorio
private void initialize(){
//TODO METODO INITIALIZE
PlaceList favoritos = new PlaceList();
favoritos.setName("Default");
favoritos.setDescription("Lista de favoritos por defecto");
Place p1 = new Place(-2354.4654, 13.5984);
p1.setEtiqueta("lugar1");
Place p2 = new Place(589.15,354.446);
p2.setEtiqueta("lugar2");
favoritos.addPlace(p1);
favoritos.addPlace(p2);
repository.put(favoritos);
PlaceList admin = new PlaceList();
admin.setName("Admin");
admin.setDescription("Lista de favoritos de Admin");
Place p11 = new Place(4564.65846,153.21);
p11.setEtiqueta("lugar_abandonado");
Place p12 = new Place(-654654.5345,-65465.353543,Calendar.getInstance(),5);
p12.setEtiqueta("playa_cristalina");
Place p13 = new Place(45456.654,4564.654,Calendar.getInstance(),9);
p13.setEtiqueta("monumento_importante");
admin.addPlace(p11);
admin.addPlace(p12);
admin.addPlace(p13);
repository.put(admin);
}
//TODO A�adir anotaciones
// @GET
// @Produces("application/json")
// public Collection<PlaceList> getAll(){
// return repository.getAll();
// }
//Si
@GET
@Produces("application/json")
public Collection<PlaceList> getAllPlaceList(){
return repository.getAll();
}
//Si
@GET
@Produces("application/json")
@Path("/{user}")
public PlaceList getPlaces(@PathParam("user")String user){
PlaceList list = repository.get(user);
if (list == null) {
throw new NotFoundException("El "+user+" no ha sido encontrado");
}
return list;
}
//Si
@POST
@Consumes("application/json")
@Produces("application/json")
public Response addPlacelist(@Context UriInfo uriInfo, PlaceList placelist) {
if (placelist.getName() == null || "".equals(placelist.getName()))
throw new BadRequestException("El nombre del usuario no debe ser nulo");
repository.put(placelist);
// Builds the response. Returns the playlist the has just been added.
UriBuilder ub = uriInfo.getAbsolutePathBuilder().path(this.getClass(), "getPlaces");
URI uri = ub.build(placelist.getName());
ResponseBuilder resp = Response.created(uri);
resp.entity(placelist);
return resp.build();
}
//Si
@PUT
@Path("/{user}")
@Consumes("application/json")
public Response updatePlacelist(@PathParam("user") String user, PlaceList placelist) {
PlaceList oldplacelist = repository.get(user);
if (oldplacelist == null) {
throw new NotFoundException("El usuario "+user+" no fue encontrado");
}
if (!oldplacelist.getName().equals(placelist.getName())) {
return Response.status(javax.ws.rs.core.Response.Status.CONFLICT).build();
}
repository.put(placelist);
return Response.noContent().build();
}
//Si
@DELETE
@Path("/{user}")
public void removePlacelist(@PathParam("user") String name) {
PlaceList toberemoved=repository.get(name);
if (toberemoved == null)
throw new NotFoundException("El usuario "+name+" no fue encontrado");
else
repository.remove(toberemoved);
}
//A�adido
//Si
@GET
@Path("/{user}/{etiqueta}")
@Produces("application/json")
public Place getPlace(@PathParam("user") String user, @PathParam("etiqueta")String etiqueta){
Place res = null;
for(Place p: getPlaces(user).getPlaces()){
if(p.getEtiqueta().equals(etiqueta)){
res = p;
}
}
if(res==null){
throw new NotFoundException("El lugar \""+etiqueta+"\" no se ha encontrado en la lista del usuario "+user+" o el usuario"+user+" no existe");
}
return res;
}
//Si
@POST
@Path("/{user}")
@Consumes("application/json")
@Produces("application/json")
public Response addPlace(@Context UriInfo uriInfo,@PathParam("user") String user, Place Place)
{
if(Place.getEtiqueta()==null || "".equals(Place.getEtiqueta()))
throw new BadRequestException("El nombre del lugar no debe ser nulo");
getPlaces(user).add(Place);
UriBuilder ub = uriInfo.getAbsolutePathBuilder().path(this.getClass(),"getPlace");
URI uri = ub.build(user,Place.getEtiqueta());
ResponseBuilder res = Response.created(uri);
return res.build();
// TODO: Implement method
}
// Si
@PUT
@Path("/{user}/{etiqueta}")
@Consumes("application/json")
@Produces("application/json")
public Response updatePlace(@Context UriInfo uriInfo,@PathParam("user") String user, @PathParam("etiqueta") String PlaceEtiqueta,Place Place)
{
Place oldPlace = getPlace(user, PlaceEtiqueta);
if(Place == null){
throw new NotFoundException(PlaceEtiqueta+"no fue encontrado en "+user);
}
if(! oldPlace.getEtiqueta().equals(Place.getEtiqueta())){
return Response.status(javax.ws.rs.core.Response.Status.CONFLICT).build();
}
oldPlace.setDia(Place.getDia());
oldPlace.setHora(Place.getHora());
oldPlace.setCoordX(Place.getCoordX());
oldPlace.setCoordY(Place.getCoordY());
return Response.noContent().build();
// TODO: Implement method
}
// Si
@DELETE
@Path("/{user}/{etiqueta}")
public void removePlace(@PathParam("user") String user, @PathParam("etiqueta") String etiqueta){
Place s= getPlace(user, etiqueta);
getPlaces(user).getPlaces().remove(s);
// TODO: Implement method
}
}